//priority 89
/**
 * 等级的词条组实现了等级词条的生成
*/
function RandomEntryGroup(name) {
    BaseEntryGroup.call(this, name)
}

// 建立原型链
RandomEntryGroup.prototype = Object.create(BaseEntryGroup.prototype);
RandomEntryGroup.prototype.constructor = RandomEntryGroup;

//词条组进行初始化时候调用
RandomEntryGroup.prototype.apply = function (nbt, name, monster) {
    try {
        let UsdEntrys = []
        this.Entrys.forEach((entry) => {
            if (entry instanceof RandomEntry && entry.CanUse(nbt, monster)) {
                UsdEntrys.push(entry)
            }
        })
        let Level_Data = nbt.get("level")
        let cp = parseInt(Level_Data.get("level"))
        let num = UsdEntrys.length
        let EnableEntrys = {}
        while (true) {
            let r = parseInt(Math.random() * num)
            let level = EnableEntrys[UsdEntrys[r].getName()]
            if (level == undefined) {
                level = 1
            } else {
                level += 1
            }
            EnableEntrys[UsdEntrys[r].getName()] = level
            cp = cp - UsdEntrys[r].LevelCost(nbt, monster, level)
            if (cp <= 0) break
        }
        for (let i in EnableEntrys) {
            let level = EnableEntrys[i]
            let entry = global.EntryHelper.AllEntry[i]
            entry.SaveData(nbt, monster, level)
            entry.ChangeName(nbt, name, monster, level)
        }
    } catch (error) {
        console.log(this.Entrys)
        console.log(nbt, name, monster, error)
    }
}

var a = new RandomEntryGroup("random_group").init()
global.EntryHelper.RegisterEntryGroup(a)